'''
Created on 09/04/2009

@author: eh01
'''
from yarra import xmlconstants
from yarra.datastore import dsconstants, dsofactory
from yarra.datastore.dsstub import DSStub
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
from yarra.logindecorator import loginRequired
import httplib
import re


class HTTPRequestDELETENodeMessageHandler(BaseEventHandler):

    @loginRequired
    def do(self, data):
        dso = data
        http_request = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_HTTP_REQUEST).get_literal()
        lock = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_LOCK).get_literal()                    
        event_type = eventconstants.HTTP_RESPONSE_EVENT_TYPE
        
        matches = re.match('/node/(\d+)/(\d+)$', http_request.path)
        
        node_id = int(matches.group(1))
        message_id = int(matches.group(2))
                
        ds_stub = DSStub()
        ds_stub.delete_message(node_id, message_id)

        # create new dso        
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               xmlconstants.XML_CONTENT_TYPE, 
                                                               '', 
                                                               http_request, 
                                                               lock)
        event = Event(event_type, new_dso)
        EventManager().add_event(event)                